﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using InfFront.UICore;

namespace InfFront.InfoSources.Base
{
    /// <summary>
    /// Base IInfoFieldGroup implementation
    /// </summary>
    [ToolboxItem(false), DesignTimeVisible(false)]
    public partial class InfoFieldGroup : IInfoFieldGroup, IComponent, IDisposable, IInfoSourceOwnedObject
    {
        #region Private members
        private string title = string.Empty;
        private string name = string.Empty;
        private string comment = string.Empty;
        private int order = 99;
        private InfoSource owner;
        private ISite site;
        #endregion

        #region Initialization/Construction
        public InfoFieldGroup()
        {
            Init();
        }

        public InfoFieldGroup(IContainer container)
        {
            container.Add(this);
            Init();
        }

        private void Init()
        {
        }
        #endregion Initialization/Construction

        #region Public properties

        [Browsable(false)]
        public InfoSource Owner
        {
            get { return owner; }
        }

        void IInfoSourceOwnedObject.SetOwner(InfoSource value)
        {
            owner = value;
        }

        #endregion Public properties

        [Browsable(false)]
        public string Name
        {
            get
            {
                string result = name;
                if (string.IsNullOrEmpty(result))
                    result = Site.Name;
                if (result == null)
                    result = string.Empty;
                return result;
            }
            set
            {
                name = value;
                if (name == null)
                    name = string.Empty;
            }
        }

        [Category("General"), DefaultValue("")]
        public string Title
        {
            get { return title; }
            set { title = value; }
        }

        [Category("General"), DefaultValue("")]
        public string Comment
        {
            get { return comment; }
            set { comment = value; }
        }

        [Category("General"), DefaultValue(99), Browsable(false)]
        public int Order
        {
            get { return order; }
            set { order = value; }
        }

        #region IInfoFieldGroup Members

        string IInfoFieldGroup.GetName()
        {
            return Name;
        }

        string IInfoFieldGroup.GetTitle()
        {
            return Title;
        }

        string IInfoFieldGroup.GetComment()
        {
            return Comment;
        }

        int IInfoFieldGroup.GetOrder()
        {
            return Order;
        }

        #endregion

        #region IDisposable Members

        void IDisposable.Dispose()
        {
            lock (this)
            {
                if ((site != null) && (site.Container != null))
                {
                    site.Container.Remove(this);
                }
                EventHandler handler = Disposed;
                if (handler != null)
                    handler(this, EventArgs.Empty);
            }
        }

        #endregion

        #region IComponent Members

        public event EventHandler Disposed;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false)]
        public ISite Site
        {
            get { return site; }
            set { site = value; }
        }

        #endregion
    }

    /// <summary>
    /// Base IInfoFieldGroupList implementation
    /// </summary>
    public class InfoFieldGroupList : InfoSourceOwnedListOf<InfoFieldGroup>
    {
        public InfoFieldGroupList(InfoSource owner)
            : base(owner)
        {
        }
    }
}
